// pages/gallery/gallery.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageUrl: [],
    pageSize: 10,
    pageNumber: 1,
    id: '',
  },
  choice(e) {
    var that = this
    var index = e.currentTarget.dataset.index;
    var em = that.data.imageUrl
    for (let i in em) {
      if (i == index) {
        em[i].flag = true
      } else {
        em[i].flag = false
      }
    }
    that.setData({
      imageUrl: em
    })
  },
  getimgs() {
    var that = this
    app.req({
      url: "wechat/activity/getPictureListByType",
      method: "post",
      data: {
        type: 17,
        pageSize: 10,
        pageNumber: 1
      },
      success: function (res) {
        if (200 == res.code) {
          console.log("获取相册列表", res)
          for (let i in res.data.rows) {
            if (i==0) {
              res.data.rows[0].flag = true
            } else {
              res.data.rows[i].flag = false
            }
          }
          console.log('列表', res.data.rows)
          that.setData({
            imageUrl: res.data.rows
          })
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  cancel() {
    wx.navigateBack({})
  },
  previewImg: function (e) {
    var index = e.currentTarget.dataset.index;
    var imgArr = [];
    console.log('图片地址', this.data.imageUrl)
    for (var i = 0; i < this.data.imageUrl.length; i++) {
      imgArr.push(this.data.imageUrl[i].url);
    };
    console.log(imgArr);
    wx.previewImage({
      current: imgArr[index],     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  save() {
    var that = this
    var em = that.data.imageUrl
    var url = ''
    for (let i in em) {
      if (em[i].flag) {
        url = em[i].url
      }
    }
    app.req({
      url: "orgmini/org/updateOrg",
      data: {
        id: that.data.id,
        coverUrl: url
      },
      success: function (res) {
        if (200 == res.code) {
          wx.navigateBack();
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none"
          })
        }
      }
    });
    // let pages = getCurrentPages(),
    //   prevPage = pages[pages.length - 2];
    // prevPage.setData({
    //   'act2.actPoster': url
    // })
    // wx.navigateBack({})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    this.getimgs()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})